package model

import (
	"context"
	"database/sql"
	"fmt"
	"portal/internal/errorx"

	"github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"github.com/zeromicro/go-zero/core/stringx"
)

var _ AwAccountsModel = (*customAwAccountsModel)(nil)

var accountInsertCol = stringx.Remove(awAccountsFieldNames, "id")

type (
	// AwAccountsModel is an interface to be customized, add more methods here,
	// and implement the added methods in customAwAccountsModel.
	AwAccountsModel interface {
		awAccountsModel
		withSession(session sqlx.Session) AwAccountsModel
		BulkInsert(context context.Context, session sqlx.Session, account []*AwAccounts) error
		InsertGetId(ctx context.Context, session sqlx.Session, data *AwAccounts) (int64, error)
		SelectOneByUname(ctx context.Context, session sqlx.Session, Uname string) (*AwAccounts, error)
		Trans(ctx context.Context, fn func(context context.Context, session sqlx.Session) error) error
		// FindListByQid(ctx context.Context,session sqlx.Session,qId int64)([]*AwAccounts,error)
	}

	customAwAccountsModel struct {
		*defaultAwAccountsModel
	}
)

// NewAwAccountsModel returns a model for the database table.
func NewAwAccountsModel(conn sqlx.SqlConn) AwAccountsModel {
	return &customAwAccountsModel{
		defaultAwAccountsModel: newAwAccountsModel(conn),
	}
}

func (m *customAwAccountsModel) withSession(session sqlx.Session) AwAccountsModel {
	return NewAwAccountsModel(sqlx.NewSqlConnFromSession(session))
}

func (m *customAwAccountsModel) BulkInsert(context context.Context, session sqlx.Session, accounts []*AwAccounts) error {
	conn := sqlx.NewSqlConnFromSession(session)
	query := fmt.Sprintf("insert into public.aw_account (username,password,create_at,update_at,role_id,avatar,introduction,name,sex,department,location,mail) values (? ,? ,?, ?, ? ,? ,?, ?,? ,? ,?, ?)")
	bulk, err := sqlx.NewBulkInserter(conn, query)
	if err != nil {
		return errorx.NewBizError(500, "批量插入问题选项失败")
	}
	defer bulk.Flush()
	for _, val := range accounts {
		fmt.Println("插入的数据是：", val)
		bulk.Insert((*val).Username, (*val).Password, (*val).CreatedAt, (*val).UpdatedAt, (*val).RoleId, (*val).Avatar, (*val).Introduction, (*val).Name, (*val).Sex, (*val).Department, (*val).Location, (*val).Mail)
	}
	return nil
}

func (m *customAwAccountsModel) SelectOneByUname(ctx context.Context, session sqlx.Session, Uname string) (*AwAccounts, error) {
	query, args, err := squirrel.Select("*").From(m.table).Where("username = ?", Uname).Limit(1).ToSql()
	if err != nil {
		return nil, err
	}
	account := &AwAccounts{}
	if session != nil {
		err = session.QueryRowCtx(ctx, account, query, args...)
	} else {
		err = m.conn.QueryRowCtx(ctx, account, query, args...)
	}
	return account, err

}

func (m *customAwAccountsModel) InsertGetId(ctx context.Context, session sqlx.Session, data *AwAccounts) (int64, error) {
	query, args, err := squirrel.Insert(m.table).Columns(accountInsertCol...).
		Values(data.Username, data.Password, data.CreatedAt, data.UpdatedAt, data.RoleId, data.Avatar, data.Introduction, data.Name, data.Sex, data.Department, data.Location, data.Mail).
		ToSql()
	if err != nil {
		return 0, err
	}
	var sqlRes sql.Result
	if session != nil {
		sqlRes,err = session.ExecCtx(ctx, query, args...)
	} else {
		sqlRes,err = m.conn.ExecCtx(ctx, query, args...)
	}
	if err != nil {
		return 0, err
	}
	id,err := sqlRes.LastInsertId()

	return id, err
}

// func (m *customAwAccountsModel)FindListByQid(ctx context.Context,session sqlx.Session,qId int64)([]*AwAccounts,error){
// 	builder := m.SelectBuilder().Where(squirrel.Eq{"question_id":qId})
// 	query,values,err := builder.ToSql()
// 	if err != nil {
// 		return nil,err
// 	}
// 	var options []*AwAccounts
// 	err = m.conn.QueryRowsPartialCtx(ctx,&options,query,values...)
// 	if err != nil {
// 		return nil,err
// 	}
// 	return options,nil
// }

func (m *customAwAccountsModel) Trans(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error {
	return m.conn.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
		return fn(ctx, session)
	})
}